package com.sgcc.operate.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.common.datascope.annotation.DataScope;
import com.sgcc.operate.domain.OIndiContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.operate.domain.OSchemeForm;
import com.sgcc.operate.service.IOSchemeFormService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 运营方案制定Controller
 * 
 * @author sgcc
 * @date 2021-03-23
 */
@RestController
@RequestMapping("/oschemeform")
public class OSchemeFormController extends BaseController
{
    @Autowired
    private IOSchemeFormService oSchemeFormService;

    /**
     * 查询运营方案制定列表
     */
    //@PreAuthorize(hasPermi = "operate:oschemeform:list")
    @GetMapping("/list")
    @DataScope(deptAlias = "o2")
    public TableDataInfo list(OSchemeForm oSchemeForm)
    {
        startPage();
        List<OSchemeForm> list = oSchemeFormService.selectOSchemeFormList(oSchemeForm);
        return getDataTable(list);
    }

    /**
     * 导出运营方案制定列表
     */
    //@PreAuthorize(hasPermi = "operate:oschemeform:export")
    @Log(title = "运营方案制定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @DataScope(deptAlias = "o2")
    public void export(HttpServletResponse response, OSchemeForm oSchemeForm) throws IOException
    {
        List<OSchemeForm> list = oSchemeFormService.selectOSchemeFormList(oSchemeForm);
        ExcelUtil<OSchemeForm> util = new ExcelUtil<OSchemeForm>(OSchemeForm.class);
        util.exportExcel(response, list, "form");
    }

    /**
     * 获取运营方案制定详细信息
     */
    //@PreAuthorize(hasPermi = "operate:oschemeform:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(oSchemeFormService.selectOSchemeFormById(id));
    }
    /**
     * 获取运营方案制定详细信息
     */
    //@PreAuthorize(hasPermi = "operate:oschemeform:query")
    @GetMapping(value = "getOSchemeNum")
    public AjaxResult getOSchemeNum()
    {
        return oSchemeFormService.getOSchemeNum();
    }

    /**
     * 新增运营方案制定
     */
    //@PreAuthorize(hasPermi = "operate:oschemeform:add")
    @Log(title = "运营方案制定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OSchemeForm oSchemeForm)
    {
        return toAjax(oSchemeFormService.insertOSchemeForm(oSchemeForm));
    }

    /**
     * 修改运营方案制定
     */
    //@PreAuthorize(hasPermi = "operate:oschemeform:edit")
    @Log(title = "运营方案制定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OSchemeForm oSchemeForm)
    {
        return toAjax(oSchemeFormService.updateOSchemeForm(oSchemeForm));
    }

    /**
     * 删除运营方案制定
     */
    //@PreAuthorize(hasPermi = "operate:oschemeform:remove")
    @Log(title = "运营方案制定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(oSchemeFormService.deleteOSchemeFormByIds(ids));
    }

    /**
     * 启用或者作废，运营方案制定
     */
    @PostMapping(value = "beginOrEnd/{schemestatus}/{ids}")
    public AjaxResult deleteAll(@PathVariable("ids")String ids,@PathVariable("schemestatus")String schemestatus) {
        String idArray[] =ids.split(",");
        for(String id : idArray){
            OSchemeForm oSchemeForm = oSchemeFormService.selectOSchemeFormById(id);
            if ("02".equals(oSchemeForm.getSchemestatus())&&"02".equals(schemestatus)) {
                return AjaxResult.error("该运营方案已开启");
            }
			if ("03".equals(oSchemeForm.getSchemestatus())&&"03".equals(schemestatus)) {
                return AjaxResult.error("该运营方案已作废");
			}

            if ("03".equals(oSchemeForm.getSchemestatus())&&"02".equals(schemestatus)) {
                return AjaxResult.error("已作废运营方案不能开启");
            }
            oSchemeForm.setSchemestatus(schemestatus);
            oSchemeForm.setOindiContentList(null);
            oSchemeFormService.updateOSchemeForm(oSchemeForm);
        }
        return AjaxResult.success("操作成功");
    }

    /**
     * 删除运营方案制定
     */
    @PostMapping("deleteOindiContent")
    public AjaxResult remove(OIndiContent oIndiContent)
    {
        return toAjax(oSchemeFormService.deleteOindiContent(oIndiContent));
    }


    /**
     * 查询运营方案制定列表
     */
    //@PreAuthorize(hasPermi = "operate:oschemeform:list")
    @GetMapping("/actiListData")
    @DataScope(deptAlias = "d")
    public TableDataInfo actiListData(OSchemeForm oSchemeForm)
    {
        startPage();
        List<OSchemeForm> list = oSchemeFormService.actiListData(oSchemeForm);
        return getDataTable(list);
    }




}
